using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using LoanApprovalClient.LoanDataService;
using LoanApprovalClient.CustomerService;
using LoanApprovalClient.LoanCalculationService;

namespace LoanApprovalClient.BLApproveAdmin
{
    public class BLApproveAdmin
    {
        public Page currentPage = null;
        const int SESSION_TIMEOUT = 2; // minutes

        private Hashtable loggedInSessions
        {
            get{
                if (currentPage.Application["LoggedInSessions"] == null){
                    currentPage.Application["LoggedInSessions"] = new Hashtable();
                }
                return currentPage.Application["LoggedInSessions"] as Hashtable; 
            }
        }

        internal bool adminLogin(string pUsername, string pPassword, out string pSessionKey)
        {
            if (!string.IsNullOrEmpty(pUsername) && !string.IsNullOrEmpty(pPassword))
            {
                if (  (pUsername.Equals("donald") && pPassword.Equals("password") )
                    || (pUsername.Equals("swapna") && pPassword.Equals("password") )
                    || (pUsername.Equals("paul") && pPassword.Equals("password") )
                ){
                    pSessionKey = HttpContext.Current.Session.SessionID;
                    loggedInSessions[HttpContext.Current.Session.SessionID] = DateTime.Now;

                    // clean up hashtbale
                    List<string> keysToRemove = new List<string>();
                    foreach (string dtKey in loggedInSessions.Keys)
                    {
                        if (((DateTime)loggedInSessions[dtKey]).AddMinutes(SESSION_TIMEOUT) < DateTime.Now)
                        {
                            keysToRemove.Add(dtKey);
                        }
                    }
                    foreach (string key in keysToRemove)
                    {
                        loggedInSessions.Remove(key);
                    }

                    return true;
                }
            }
            pSessionKey = string.Empty;
            return false;
        }

        internal bool checkIfUserLoggedIn(string pSessionKey)
        {
            DateTime dt = (DateTime)loggedInSessions[pSessionKey] ;
            if (dt != null && dt.AddMinutes(SESSION_TIMEOUT) > DateTime.Now)
            {
                // Reset the timeout everytime a valid user hits the page.
                loggedInSessions[pSessionKey] = DateTime.Now;
                return true;
            }
            return false;
        }

        internal List<LoanApprovalClient.LoanDataService.Application> getPendingApplications()
        {
            //LoanDataService.Service service = new LoanApprovalClient.LoanDataService.Service();
            //LoanApprovalClient.LoanDataService.Application app = service.getPendingLoanApprovalList("Pending");
            try
            {
                List<Application> list = new List<Application>();
                Application newApp = new Application();
                newApp.AnnualExpense = 100;
                newApp.AnnualIncome = 50000;
                newApp.ApplicationID = 1;
                newApp.ApplicationStatus = "Pending";
                newApp.AssetValue = 2000;
                newApp.Comments = "No comments yet";
                newApp.CustomerID = 1;
                newApp.QuoteID = 1;
                newApp.StatusUpdateDate = DateTime.Now;
                newApp.StatusUpdateUserID = 1;
                list.Add(newApp);
                list.Add(newApp);
                list.Add(newApp);
                return list;
            }
            catch (Exception pEx)
            {
            }
            return new List<Application>();
        }

        internal Application getLoanApplication(int loanAppID)
        {
            try
            {
                LoanDataService.ServiceSoapClient service = new LoanApprovalClient.LoanDataService.ServiceSoapClient();
                return service.getLoanApplication(loanAppID);
            }catch(Exception pEx){
                return null;
            }
        }

        internal Customer getCustomer( int pCustomerID)
        {
            try
            {
                CustomerService.ServiceSoapClient service = new CustomerService.ServiceSoapClient();
                return service.getCustomerDetailsByID(pCustomerID);
            }
            catch (Exception pEx)
            {
                return null;
            }
        }

        internal Quote getQuote(int pQuoteID)
        {
            try
            {
                LoanCalculationService.LoanCalculationServiceSoapClient service = new LoanCalculationService.LoanCalculationServiceSoapClient();
                return service.getLoanQuote(pQuoteID);
            }
            catch (Exception pEx)
            {
                return null;
            }
        }

        internal void rejectLoanApplication(ref Application pApp, ref Customer pCustomer, ref Quote pQuote)
        {
            pApp.ApplicationStatus = "Rejected";
            updateLoanApplication(ref pApp, ref pCustomer, ref pQuote);
        }

        internal void approveLoanApplication(ref Application pApp, ref Customer pCustomer, ref Quote pQuote)
        {
            pApp.ApplicationStatus = "Approved";
            updateLoanApplication(ref pApp, ref pCustomer, ref pQuote);
        }
        
        private void updateLoanApplication(ref Application pApp, ref Customer pCustomer, ref Quote pQuote)
        {
            try
            {
                // Update Customer details 
                if (pCustomer != null)
                {
                    CustomerService.ServiceSoapClient customerService = new CustomerService.ServiceSoapClient();
                    customerService.updateCustomerDetails(pCustomer);
                }
                // Update Quote details
                if (pQuote != null)
                {
                    LoanCalculationService.LoanCalculationServiceSoapClient loanCalcService = new LoanCalculationService.LoanCalculationServiceSoapClient();
                    loanCalcService.updateLoanQuote(
                        pQuote.QuoteID, pQuote.CustomerID, pQuote.PrptyType, pQuote.PrptySize, pQuote.PrptyYearBuilt, pQuote.PrptyAddressLine1, pQuote.PrptyAddressLine2, pQuote.PrptySuburb, pQuote.PrptyState, pQuote.PrptyPostcode, pQuote.PrptyValue, pQuote.LoanAmount, pQuote.LoanTerm, pQuote.LoanIntrest, pQuote.LoanInsurance, pQuote.LoanMRepayment, pQuote.QuoteDate);

                }
                // Update Quote details
                if (pApp != null)
                {
                    LoanDataService.ServiceSoapClient loanService = new LoanDataService.ServiceSoapClient();
                    loanService.updateLoanApplication(pApp);
                }
            }
            catch (Exception pEx)
            {
                throw pEx;
            }
        }
    }
}
